<?php
	include "./utils/auth.php";
	// echo $_POST["lineupamount"];
	// echo $_POST["reserveamount"];
	//echo $_POST["cid"];
	 
	 if(!(isset($_POST["cid"])))
	 {
		return;
	 }
	 if(!(isset($_POST["lineupamount"])))
	 {
		return;
	 }
	 if(!(isset($_POST["reserveamount"])))
	 {
		return;
	 }
	
	try
	{
		$db->query("START TRANSACTION");
		
		$checkstatus= '
		SELECT payedout
		FROM wottools.wotcwchannel
		WHERE channelid = ?
		FOR UPDATE
		';
		if(!($checkstmt = $db->prepare($checkstatus)))
		{
			throw new Exception("cannot prep statement ".$checkstatus);
		}
		if(!($checkstmt->bind_param("i",$_POST["cid"])))
		{
			throw new Exception("cannot bind param ".$_POST["cid"]);
		}		
		if(!($checkstmt->bind_result($paidout)))
		{
			throw new Exception("cannot bind result");
		}
		
		if(!($checkstmt->execute()))
		{
			throw new Exception("cannot update");
		}
		
		$checkstmt->fetch();
		$checkstmt->close();
		if($paidout>0)
		{
			echo "allready paid out";
			return;
		}
		
		
		
		$payed = '
		UPDATE wottools.wotcwchannel
		SET payedout=1
		WHERE channelid = ?;
		';
		if(!($payedstmt = $db->prepare($payed)))
		{
			throw new Exception("cannot prep statement ".$payed);
		}
		if(!($payedstmt->bind_param("i",$_POST["cid"])))
		{
			throw new Exception("cannot bind param ".$_POST["cid"]);
		}
		if(!($payedstmt->execute()))
		{
			throw new Exception("cannot update");
		}
		$payedstmt->close();
		
		
		//////////////////////////////////////lineup
		if($_POST["lineupamount"]>0)
		{
		
			$lineup = 
			"SELECT DISTINCT id
			FROM wottools.combatants c 
				INNER JOIN wottools.users u ON ( c.userid = u.`id`   )  
				INNER JOIN wottools.wotcwchannel w ON ( c.concernedchannel = w.`channelid`  AND c.fc = w.fc  )  
			WHERE w.`channelid`= ? AND c.reserve= ? ;";
			
			if(!($stmt = $db->prepare($lineup)))
			{
				throw new Exception("could not prep statement ".$lineup);
			}
			$reserve = 0;
			if(!($stmt->bind_param("ii",$_POST['cid'],$reserve)))
			{
				throw new Exception("cannot bind params to getlineupstmt");
			}
			
			if(!($stmt->execute()))
			{
				throw new Exception("cannot get lineup");
			}
			
			
			if(!($stmt->store_result()))
			{
				throw new Exception("cannot store result");
			}
			
			if(!($stmt->bind_result($id)))
			{
				throw new Exception("cannot bind result");
			}
			
			$inserttransaction = '
			INSERT INTO wottools.pendingtransactions
			(  `REMITTEE`, `PAYER`, `AMOUNT`, `REASON`, `REASONID`) 
			VALUES 
			( ?, ?, ?, ?, ? );
			';
			if(!($istmt = $db->prepare($inserttransaction)))
			{
				throw new Exception("could not prep ".$inserttransaction.$db->error);
			}
			
			while($stmt->fetch())
			{
				$one = 1;
				$tone=1;
				if(!($istmt->bind_param("iiiii",$id, $one, $_POST["lineupamount"], $tone, $_POST["cid"])))
				{
					throw new Exception("cannot bind params for lineup");
				}
				if(!($istmt->execute()))
				{
					throw new Exception("cannot execute payment");
				}
			}
			$istmt->close();
			$stmt->free_result();
			$stmt->close();
		}	
		
		
		///////////////////////////////////////////////////////////////////////reserve
		if($_POST["reserveamount"]>0)
		{
			$lineup = 
			"SELECT DISTINCT id
			FROM wottools.combatants c 
				INNER JOIN wottools.users u ON ( c.userid = u.`id`  )  
				INNER JOIN wottools.wotcwchannel w ON ( c.concernedchannel = w.`channelid`   AND c.fc = w.fc) 
			WHERE w.`channelid`= ? AND c.reserve= ?;";
			
			if(!($stmt = $db->prepare($lineup)))
			{
				throw new Exception("could not prep statement ".$lineup);
			}
			$reserve = 1;
			if(!($stmt->bind_param("ii",$_POST['cid'],$reserve)))
			{
				throw new Exception("cannot bind params to getlineupstmt");
			}
			
			if(!($stmt->execute()))
			{
				throw new Exception("cannot get lineup");
			}
			
			
			if(!($stmt->store_result()))
			{
				throw new Exception("cannot store result");
			}
			
			if(!($stmt->bind_result($id)))
			{
				throw new Exception("cannot bind result");
			}
			
			$inserttransaction = '
			INSERT INTO wottools.pendingtransactions
			(  `REMITTEE`, `PAYER`, `AMOUNT`, `REASON`, `REASONID`) 
			VALUES 
			( ?, ?, ?, ?, ? );
			';
			if(!($istmt = $db->prepare($inserttransaction)))
			{
				throw new Exception("could not prep ".$inserttransaction.$db->error);
			}
			
			while($stmt->fetch())
			{
				$one = 1;
				$tone=1;
				if(!($istmt->bind_param("iiiii",$id, $one, $_POST["reserveamount"], $tone, $_POST["cid"])))
				{
					throw new Exception("cannot bind params for lineup");
				}
				if(!($istmt->execute()))
				{
					throw new Exception("cannot execute payment");
				}
			}
			$istmt->close();
			$stmt->free_result();
			$stmt->close();
		}
		echo"commiting";
		$db->query("COMMIT");
	}
	catch(Exception $e)
	{
		echo $e." DBERROR=".$db->error;
		 $db->query("ROLLBACK");
		 
	}
?>